package com.jingdianjichi.subject.application.controller;

import com.alibaba.fastjson.JSON;
import com.jingdianjichi.subject.application.convert.SubjectCategoryDTOConverter;
import com.jingdianjichi.subject.application.dto.SubjectCategoryDTO;
import com.jingdianjichi.subject.application.service.SubjectCategoryApplicationService;
import com.jingdianjichi.subject.domain.entity.SubjectCategoryBO;
import com.jingdinajichi.subject.common.entity.Result;
import com.google.common.base.Preconditions;


import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;


/**
 * @author zwy
 * @version 1.0
 * @description: SubjectCategoryController
 * @date 2025/7/5 16:22
 */
@RestController
@RequestMapping("/subject/category")
@Slf4j
public class SubjectCategoryController {
    @Resource
    private SubjectCategoryApplicationService subjectCategoryApplicationService;
    @PostMapping("/add")
    public Result<Boolean> add(@RequestBody SubjectCategoryDTO subjectCategoryDTO) {
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.add.bo:{}", JSON.toJSON(subjectCategoryDTO));
        }
        // 当校验失败时，Preconditions会抛出IllegalArgumentException
        // 这个异常会被我们新建的 GlobalExceptionHandler 捕获并处理
        Preconditions.checkNotNull(subjectCategoryDTO.getCategoryType(),"分类类型不能为空");
        Preconditions.checkNotNull(subjectCategoryDTO.getCategoryName(),"分类名称不能为空");

        SubjectCategoryBO subjectCategoryBO = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
        subjectCategoryApplicationService.add(subjectCategoryBO);
        return Result.ok(true);
    }

    /**
     * `查询题目大类
     * @return
     */
    @GetMapping("/queryPrimaryCategory")
    public Result<List<SubjectCategoryDTO>> queryPrimaryCategory(){
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.queryPrimaryCategory");
        }
        SubjectCategoryBO subjectCategoryBO = new SubjectCategoryBO();
        subjectCategoryBO.setCategoryType(1);
            List<SubjectCategoryBO>  subjectCategoryBOS  = subjectCategoryApplicationService.queryPrimaryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.INSTANCE.convertBOToDTO(subjectCategoryBOS);
            return Result.ok(subjectCategoryDTOS);
        }

   /**
    * 查询题目分类小类
    */
    @PostMapping("/queryCategoryByPrimary")
    public Result<List<SubjectCategoryDTO>> queryCategoryByPrimary(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.queryCategoryByPrimary.bo:{}", JSON.toJSON(subjectCategoryDTO));
        }
            SubjectCategoryBO subjectCategoryBO  = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            List<SubjectCategoryBO>  subjectCategoryBOS  = subjectCategoryApplicationService.queryCategory(subjectCategoryBO);
            List<SubjectCategoryDTO> subjectCategoryDTOS = SubjectCategoryDTOConverter.INSTANCE.convertBOToDTO(subjectCategoryBOS);
            return Result.ok(subjectCategoryDTOS);

        }
    @PostMapping("/update")
    public Result<Boolean> update(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.update.DTO:{}", JSON.toJSON(subjectCategoryDTO));
        }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类id不能为空");
            SubjectCategoryBO subjectCategoryBO  = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            boolean b  = subjectCategoryApplicationService.update(subjectCategoryBO);
            return Result.ok(b);
    }
    @PostMapping("/delete")
    public Result<Boolean> delete(@RequestBody SubjectCategoryDTO subjectCategoryDTO){
        if(log.isInfoEnabled()){
            log.info("SubjectCategoryController.delete.DTO:{}", JSON.toJSON(subjectCategoryDTO));
        }
            Preconditions.checkNotNull(subjectCategoryDTO.getId(),"分类id不能为空");
            SubjectCategoryBO subjectCategoryBO  = SubjectCategoryDTOConverter.INSTANCE.convertDTOToBO(subjectCategoryDTO);
            boolean b  = subjectCategoryApplicationService.delete(subjectCategoryBO);
            return   b ? Result.ok("删除成功"): Result.fail("删除失败");

    }

}



